package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.distributor.dto.DistributorAreaAddDTO;
import com.xbongbong.distributor.dto.DistributorAreaDelDTO;
import com.xbongbong.distributor.dto.DistributorAreaListDTO;
import com.xbongbong.distributor.dto.DistributorAreaSortDTO;
import com.xbongbong.distributor.dto.DistributorAreaUpdateDTO;
import com.xbongbong.distributor.vo.DistributorAreaAddVO;
import com.xbongbong.distributor.vo.DistributorAreaDelVO;
import com.xbongbong.distributor.vo.DistributorAreaListVO;
import com.xbongbong.distributor.vo.DistributorAreaSortVO;
import com.xbongbong.distributor.vo.DistributorAreaUpdateVO;
import com.xbongbong.paas.pojo.vo.DepartmentSimpleListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.DistributorAreaService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author mengna.deng
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/distributorArea")
public class DistributorAreaController {
    @Resource
    private DistributorAreaService distributorAreaService;

    /**
     * 区域设置列表接口
     * @param distributorAreaListDTO 关联业务DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DistributorAreaListDTO distributorAreaListDTO, BindingResult br) throws Exception {
        XbbResponse<DistributorAreaListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAreaListVO distributorAreaListVO = distributorAreaService.list(distributorAreaListDTO);
                response = new XbbResponse<>(distributorAreaListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加区域
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DistributorAreaAddDTO assistAddDTO, BindingResult br){
        XbbResponse<DistributorAreaAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAreaAddVO distributorAreaAddVO = distributorAreaService.add(assistAddDTO);
                response = new XbbResponse<>(distributorAreaAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新区域，用于重命名
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DistributorAreaUpdateDTO distributorAreaUpdateDTO, BindingResult br){
        XbbResponse<DistributorAreaUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAreaUpdateVO distributorAreaUpdateVO = distributorAreaService.update(distributorAreaUpdateDTO);
                response = new XbbResponse<>(distributorAreaUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除区域
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DistributorAreaDelDTO distributorAreaDelDTO, BindingResult br){
        XbbResponse<DistributorAreaDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAreaDelVO distributorAreaDelVO = distributorAreaService.delete(distributorAreaDelDTO);
                response = new XbbResponse<>(distributorAreaDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * sort
     * @return
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DistributorAreaSortDTO distributorAreaSortDTO, BindingResult br){
        XbbResponse<DistributorAreaSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DistributorAreaSortVO distributorAreaSortVO = distributorAreaService.sort(distributorAreaSortDTO);
                response = new XbbResponse<>(distributorAreaSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 所在地区查询，表单内用
     * @param distributorAreaListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/form/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String areaFormList(@RequestBody @Valid DistributorAreaListDTO distributorAreaListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentSimpleListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentSimpleListVO departmentSimpleListVO = distributorAreaService.areaFormList(distributorAreaListDTO);
                response = new XbbResponse<>(departmentSimpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
